﻿using MapHello.Basic;
using MapHello.Data;
using MapHello.Service;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using Windows.Devices.Geolocation;
using Windows.Storage;

namespace MapHello
{
    public class View : DataEntity
    {
        private MapHelloService _service;

        private MapHelloService Service
        {
            get
            {
                if (null == _service)
                {
                    _service = new MapHelloService();
                }
                return _service;
            }
        }

        private static View _Instance;

        public static View Instance
        {
            get
            {
                if (null == _Instance)
                {
                    _Instance = new View();
                }
                return _Instance;
            }
        }

        private View()
        {

        }

        #region Windows Live Account

        private SecurityContext _securityContext;

        public SecurityContext SecurityContext
        {
            get
            {
                if (null == _securityContext)
                {
                    _securityContext = Service.LoadSecurityContext();
                }
                return _securityContext;
            }
            set
            {
                Service.SaveSecurityContext(value);
                _securityContext = value;
            }
        }

        #endregion

        private Person _me;
        public Person Me
        {
            get
            {
                if (null == _me)
                {
                    _me = Service.LoadMe();
                }
                return _me;
            }
            set
            {
                Service.SavePerson(value);
                _me = value;
            }
        }

        private bool _isLocationTracked;
        public bool IsLocationTracked
        {
            get
            {
                var config = Service.LoadConfig(Config.IsLocationTracked);
                if (null == config)
                {
                    _isLocationTracked = false;
                }
                else
                {
                    _isLocationTracked = bool.Parse(config.Value);
                }
                return _isLocationTracked;
            }
            set
            {
                NotifyPropertyChanging("IsLocationTracked");
                _isLocationTracked = value;
                Service.SaveConfig(Config.IsLocationTracked, value.ToString(), true);
                NotifyPropertyChanged("IsLocationTracked");
            }
        }

        private ObservableCollection<Person> _friendsOnMap;
        public ObservableCollection<Person> FriendsOnMap
        {
            get
            {
                if (null == _friendsOnMap)
                {
                    _friendsOnMap = new ObservableCollection<Person>();
                }
                return _friendsOnMap;
            }
        }
    }
}
